package cn.iocoder.yudao.module.picturebook.service.bookshelf;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.bookshelf.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.bookshelf.BookShelfDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.picturebook.dal.mysql.bookshelf.BookShelfMapper;


/**
 * 用户书架关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class BookShelfServiceImpl implements BookShelfService {

    @Resource
    private BookShelfMapper bookShelfMapper;

    // ... existing code ...
    @Override
    public Long createBookShelf(BookShelfSaveReqVO createReqVO) {
        // 参数校验
        if (createReqVO == null) {
            return null;
        }

        // 插入
        BookShelfDO bookShelf = BeanUtils.toBean(createReqVO, BookShelfDO.class);
        bookShelfMapper.insert(bookShelf);

        // 添加空值检查
        if (bookShelf.getId() == null) {
            return null;
        }

        // 返回
        return bookShelf.getId();
    }

    @Override
    public void updateBookShelf(BookShelfSaveReqVO updateReqVO) {
        // 参数校验
        if (updateReqVO == null) {
            return;
        }
        if (updateReqVO.getId() == null) {
            return;
        }

        // 校验存在
        if (bookShelfMapper.selectById(updateReqVO.getId()) == null) {
            return;
        }
        // 更新
        BookShelfDO updateObj = BeanUtils.toBean(updateReqVO, BookShelfDO.class);
        bookShelfMapper.updateById(updateObj);
    }

    @Override
    public void deleteBookShelf(Long id) {
        // 参数校验
        if (id == null) {
            return;
        }

        // 校验存在
        if (bookShelfMapper.selectById(id) == null) {
            return;
        }
        // 删除
        bookShelfMapper.deleteById(id);
    }

    @Override
    public void deleteBookShelfListByIds(List<Long> ids) {
        // 参数校验
        if (ids == null || ids.isEmpty()) {
            return;
        }

        // 删除
        bookShelfMapper.deleteByIds(ids);
    }


    private void validateBookShelfExists(Long id) {
        // 参数校验
        if (id == null) {
            return;
        }

        if (bookShelfMapper.selectById(id) == null) {
            log.error("[getBookShelf][id({}) 不存在]", id);
            // 不抛出异常，仅记录日志
            return;
        }
    }

    @Override
    public BookShelfDO getBookShelf(Long id) {
        // 参数校验
        if (id == null) {
            return null;
        }

        return bookShelfMapper.selectById(id);
    }

    @Override
    public PageResult<BookShelfDO> getBookShelfPage(BookShelfPageReqVO pageReqVO) {
        // 参数校验
        if (pageReqVO == null) {
            return PageResult.empty();
        }

        return bookShelfMapper.selectPage(pageReqVO);
    }
// ... existing code ...


}